package com.jxd.ordering.controller;

import com.jxd.ordering.annotation.CheckPermission;
import com.jxd.ordering.exception.CommonEnum;
import com.jxd.ordering.model.Meal;
import com.jxd.ordering.model.ResultBody;
import com.jxd.ordering.service.IMealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @ClassName MealController
 * @Description TODO
 * @Author Ankailong
 * @Date 2022/9/28
 * @Version 1.0
 */
@RestController
public class MealController {
    @Autowired
    private IMealService mealService;

    /**
     * 新增餐品信息
     * level: 3
     * @param meal 餐品实体类
     * @return 是否新增成功
     */
    @CheckPermission("3")
    @RequestMapping("/addMeal")
    public ResultBody addMeal(@RequestBody Meal meal) {
        return mealService.addMeal(meal) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 修改餐品信息
     * level: 3
     * @param meal 餐品实体类
     * @return 是否修改成功
     */
    @CheckPermission("3")
    @RequestMapping("/editMeal")
    public ResultBody editMeal(@RequestBody Meal meal) {
        return mealService.editMeal(meal) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 批量删除餐品信息
     * level: 3
     * @param mealnos 餐品信息编号列表
     * @return 是否删除成功
     */
    @CheckPermission("3")
    @RequestMapping("/delMeal")
    public ResultBody delMeal(@RequestBody List<Integer> mealnos) {
        return mealService.delMeal(mealnos.stream().mapToInt(Integer::intValue).toArray()) ? ResultBody.success() : ResultBody.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }

    /**
     * 分页查询全部餐品信息
     * level: 1
     * @param queryMap 包含int page, int limit, String mname, double price1, double price2
     * @return 查询结果
     */
    @RequestMapping("/getMealByPage")
    public Map<String, Object> getMealByPage(@RequestBody Map<String,String> queryMap) {
        return mealService.getMealByPage(queryMap);
    }

    /**
     * 按餐品编号查询餐品信息
     * level: 1
     * @param mealno 餐品编号
     * @return 查询结果
     */
    @RequestMapping("/getMealByMealno")
    public Meal getMealByMealno(int mealno) {
        return mealService.getMealByMealno(mealno);
    }

    /**
     * 获取全部餐品信息
     * level: 1
     * @return 查询结果
     */
    @RequestMapping("/getAllMeal")
    public List<Meal> getAllMeal() {
        return mealService.getAllMeal();
    }
}
